package com.example.temp.controller;


import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.temp.common.Constants;
import com.example.temp.common.Result;
import com.example.temp.entity.AssetLend;
import com.example.temp.entity.AssetMaintain;
import com.example.temp.entity.AssetSubscribe;
import com.example.temp.service.AssetSubscribeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.net.URLEncoder;
import java.util.List;
@Tag(name = "资产申购管理")
@RestController
@RequestMapping("/subscribe")
public class AssetSubscribeController {

    @Autowired
    private AssetSubscribeService assetSubscribeService;

    /**
     * 用于json格式测试
     * @return
     */
    @Operation(summary = "列表，用于测试json格式的数据")
    @GetMapping
    public List<AssetSubscribe> findList() {
        return assetSubscribeService.list();
    }

    /**
     * 审批情况-用于翻页
     * @param pageNum
     * @param pageSize
     * @param subscibeId (defaultValue="")
     * @return
     */
    @Operation(summary = "审批情况-用于翻页")
    @GetMapping("/page")
    public Result Page(@RequestParam Integer pageNum,
                       @RequestParam Integer pageSize,
                       @RequestParam(defaultValue = "") String subscibeId){
        IPage<AssetSubscribe>page=new Page<>(pageNum,pageSize);
        QueryWrapper<AssetSubscribe> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("audit", "否");
        queryWrapper.like("subscibe_id", subscibeId);
        IPage<AssetSubscribe>assetInformationIPage=assetSubscribeService.page(page,queryWrapper);
        return Result.success(assetInformationIPage);
    }

    /**
     * 提交申请-用于添加资产申购信息
     * @param assetSubscribe
     * @return
     */
    @Operation(summary = "提交申请-用于添加资产申购信息")
    @PostMapping
    public Result save(@RequestBody AssetSubscribe assetSubscribe){
        return Result.success(assetSubscribeService.saveAssetSubscribe(assetSubscribe));
    }

    /**
     * 审批情况-用于审核是否同意申购
     * @param assetSubscribe
     * @return
     */
    @Operation(summary = "审批情况-用于添加资产申购信息")
    @PostMapping("/audit")
    public Result judgeAudit(@RequestBody AssetSubscribe assetSubscribe){
        if (assetSubscribe.getAudit().equals("是")){
            return Result.success(assetSubscribeService.autiSubscribe(assetSubscribe));
        }else {
            return Result.error(Constants.CODE_401,"权限不足");
        }
    }

    /**
     * 审批情况-用于删除指定单个资产信息
     * @param id
     * @return
     */
    @Operation(summary = "审批情况-用于删除指定单个资产信息")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(assetSubscribeService.removeById(id));
    }


    /**
     * 审批情况-用于批量删除
     * @param ids
     * @return
     */
    @Operation(summary = "审批情况-用于批量删除")
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(assetSubscribeService.removeBatchByIds(ids));
    }

    /**
     * 资产购入审批情况-导出借出信息
     * @param response
     * @throws Exception
     */
    @Operation(summary = "资产购入审批情况-导出申购信息")
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws Exception {
        // 从数据库查询出所有的数据
        List<AssetSubscribe> list = assetSubscribeService.list();

        ExcelWriter writer = ExcelUtil.getWriter(true);

        writer.write(list, true);

        // 设置浏览器响应的格式
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = URLEncoder.encode("资产申购", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");

        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        out.close();
        writer.close();

    }

}
